package com.asia5b.a5mall.home.activity

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.home.R
import com.asia5b.a5mall.home.adapter.ChoiceLanguageAdapter
import com.asia5b.a5mall.home.databinding.ActivityChoicelanguageBinding
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.config.CountrysRepository
import com.asia5b.a5mall.middleware.model.bean.ChoiceLanguageBean
import com.asia5b.a5mall.middleware.model.member.LanuageModel
import com.asia5b.a5mall.middleware.utils.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_choicelanguage.*

/**
 * Created by zhangyuncai on 2019/11/15.
 * 选择语言
 */
@Route(path = ActCons.ChoiceLanguageActivity)
class ChoiceLanguageActivity : SimpleActivity<SimpleViewModel, ActivityChoicelanguageBinding>() {
    override val viewModelClazz: Class<SimpleViewModel>
        get() = SimpleViewModel::class.java

    override fun getDataBinding(): ActivityChoicelanguageBinding {
        return ActivityChoicelanguageBinding.inflate(layoutInflater)
    }
    override var sIsDark = true

//    private val languageList by lazy {
//        //todo 2020-06-03 朱工说写死,无马来语
//        //todo 2020-06-13 我们决定用动态语言的方式
//        val mDatas = arrayListOf<ChoiceLanguageBean>()
//        mDatas.add(ChoiceLanguageBean(R.drawable.ic_country_china, getString(R.string.中文), LocalManageUtil.LANGUAGE_CN))
//        mDatas.add(ChoiceLanguageBean(R.drawable.ic_country_english, getString(R.string.英文), LocalManageUtil.LANGUAGE_EN))
//        mDatas.add(ChoiceLanguageBean(R.drawable.ic_country_malaysia, getString(R.string.马来文), LocalManageUtil.LANGUAGE_MS))
//        mDatas.add(ChoiceLanguageBean(R.drawable.ic_country_thailand, getString(R.string.泰文), LocalManageUtil.LANGUAGE_TH))
//        mDatas.add(ChoiceLanguageBean(R.drawable.ic_country_vietnamese, getString(R.string.越南文), LocalManageUtil.LANGUAGE_VI))
////        mDatas.add(ChoiceLanguageBean(R.drawable.ic_country_vietnamese, getString(R.string.philippines), LocalManageUtil.LANGUAGE_CN))
//        mDatas.add(ChoiceLanguageBean(R.drawable.ic_country_indonesia, getString(R.string.印尼文), LocalManageUtil.LANGUAGE_SU))
//        mDatas
//    }
    private val mDatas = arrayListOf<ChoiceLanguageBean>()
    private val mAdapter: ChoiceLanguageAdapter by lazy { ChoiceLanguageAdapter(mDatas) }


    override fun initView() {

        rv_choicelanguage.layoutManager = LinearLayoutManager(this)!!
        rv_choicelanguage.visibility
        rv_choicelanguage.adapter = mAdapter
        rv_choicelanguage.addItemDecoration(newDivider(mActivity))

        mAdapter.setOnItemClickListener { adapter, view, position ->
            val bean = mAdapter.getItem(position)!!
            if (bean.language != mAdapter.select_language) {
                LocalManageUtil.setAppLanguageSelect(bean.language)
                LocalManageUtil.getInstance().setConfiguration()
//                    ARouter.getInstance().build(ActCons.MainActivity)
//                            .withFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK)
//                            .navigation()
                resetMainActivity(mActivity)
            }
        }
    }

    override fun initEvent() {
    }

    override fun initData() {
        getLanuage()
    }

    fun getLanuage() {
        CountrysRepository.anyOb<ArrayList<LanuageModel>>(CountrysRepository.lanuage)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(Consumer {
                    if (!it.isNullOrEmpty()) {
                        mDatas.clear()
                        for (lanuageModel in it) {
                            when (lanuageModel.languages_abbreviation) {
                                "ms" -> {//马来语
                                    mDatas.add(ChoiceLanguageBean(lanuageModel.icon, getString(R.string.马来文), LocalManageUtil.LANGUAGE_MS))

                                }
                                "cn" -> {//中文(简体)

                                    mDatas.add(ChoiceLanguageBean(lanuageModel.icon, getString(R.string.中文), LocalManageUtil.LANGUAGE_CN))
                                }
                                "en" -> {//英语
                                    mDatas.add(ChoiceLanguageBean(lanuageModel.icon, getString(R.string.英文), LocalManageUtil.LANGUAGE_EN))

                                }
                                "th" -> {//泰语

                                    mDatas.add(ChoiceLanguageBean(lanuageModel.icon, getString(R.string.泰文), LocalManageUtil.LANGUAGE_TH))
                                }
                                "id" -> {//印尼
                                    mDatas.add(ChoiceLanguageBean(lanuageModel.icon, getString(R.string.印尼文), LocalManageUtil.LANGUAGE_SU))

                                }
                                "vi" -> {//越南
                                    mDatas.add(ChoiceLanguageBean(lanuageModel.icon, getString(R.string.越南文), LocalManageUtil.LANGUAGE_VI))

                                }
                                "fil" -> {//菲律宾
                                    //todo 暂无对应的翻译
                                }
                            }
                        }
                        mAdapter.notifyDataSetChanged()
                    }
                }, Consumer {
                    it.printStackTrace()
                }).joinComposite(mCompositeDisposable)
    }

}